Alias Syntax

In this lesson, we will learn about assigning aliases to tables and columns.

Alias syntax#

You can rename a table or a column temporarily by giving another name known as an Alias. The use of table aliases is to rename a table in a specific SQL statement. Column aliases are used to rename a table’s columns for a particular SQL query. This renaming is a temporary change and the actual table/column name does not change in the database.

Syntax of a table alias#

The basic syntax of a table alias is as follows:

SELECT column1, column2 ... columnN

FROM table_name AS alias_name

WHERE condition;

Example#

For our example, we will be using the following tables:

Customer Table

ID NAME AGE ADDRESS SALARY
1 Mark 32 Texas 50,000
2 John 25 NY 65,000
3 Emily 23 Ohio 20,000
4 Bill 25 Chicago 75,000
5 Tom 27 Washington 35,000
6 Jane 22 Texas 45,000

Orders Table

ORDER_ID DATE CUSTOMER_ID AMOUNT
100 2019-09-08 2 5000
101 2019-08-20 5 3000
102 2019-05-12 1 1000
103 2019-02-02 2 2000

As you can see in the code above, we temporarily assigned a new alias C to the CUSTOMERS table and O to the ORDERS table.

Furthermore, when working with two tables, we need to specify the table name from which the column is derived, therefore having short aliases helps us to avoid writing large names before each column name. Also, sometimes tables can have the same column names so specifying the table name before the column name helps to avoid confusion regarding which table we are referring to.

In the highlighted lines above, the query simply displays the relevant fields we have selected based on the condition that ID in the CUSTOMERS table is equal to ORDER_ID in ORDERS table. So the result-set is the group of people who have placed orders.

Syntax for a column alias#

The basic syntax of a column alias is as follows:

SELECT column_name AS alias_name

FROM table_name

WHERE condition;

EXAMPLE#

Following is the usage of a column alias.

As you can see in the output above, the column names have changed.

A column alias is particularly useful when we want to change the name of a column to one that is easier to understand for the user.

Quick quiz!#

Q

The following query will change the name of the column ID to CUSTOMER_ID

SELECT  ID , NAME AS CUSTOMER_NAME
FROM CUSTOMERS AS CUST
A)

True

B)

False


In the next lesson, we will learn to combine two different tables using joins.

The HAVING Clause
SQL Joins
Mark as Completed
Report an Issue